import { ref } from "vue";

function useGetList<TParams, KListItem>(getListFC: (params: TParams) => Promise<{
  list: KListItem[];
  page: number;
  size: number;
  total: number;
}>) {
  const list = ref<Array<KListItem>>([]) as Ref<KListItem[]>;
  const loading = ref(true);
  const size = ref(15);
  const page = ref(1);
  const total = ref(0);

  const getList = async (params: TParams) => {
    loading.value = true;
    const { list: curList, page: curPageNo, size: curPageSize, total: curTotal } = await getListFC(params);
    list.value = curList;
    page.value = curPageNo;
    size.value = curPageSize;
    total.value = curTotal;
    loading.value = false;
  };

  return {
    list,
    getList,
    loading,
    total,
    page,
    size,
  };
};

export default useGetList;